﻿using System;
using System.Collections.Generic;
using System.Linq;

using DigitalQuran.Data.Metadata;

namespace DigitalQuran.Collection
{
    /// <summary>
    /// Collection of <see cref="DigitalQuran.Bowing"/>
    /// </summary>
    public class BowingCollection : ReadOnlyCollection<Bowing>
    {
        /// <summary>
        /// Total of Bowings in Qur'an
        /// </summary>
        public const int TotalBowings = 556;

        internal BowingCollection() : this(0, TotalBowings) { }

        /// <summary>
        /// Craete new Subset of <see cref="DigitalQuran.Collection.BowingCollection"/>
        /// </summary>
        /// <param name="startIndex">First Bowing Index</param>
        /// <param name="count">Total number of Bowings to include in subset</param>
        public BowingCollection(int startIndex, int count) : base(count)
        {            
            firstId = startIndex;
        }

        int firstId = 0;

        public override Bowing this[int index]
        {
            get
            {
                if (index < Count)
                    return _Bowings[index + firstId];
                else
                    throw new IndexOutOfRangeException();
            }
        }

        public override IEnumerator<Bowing> GetEnumerator()
        {
            for (int i = 0; i < Count; i++)
            {
                yield return _Bowings[i + firstId];
            }
        }

        static Bowing[] _Bowings = new Bowing[TotalBowings];
        internal static void LoadData(IEnumerator<ValueSpan> data)
        {                        
            for (int i = 0; i < TotalBowings; i++)
            {
                data.MoveNext();
                _Bowings[i] = new Bowing(i, data.Current.LowerBound, data.Current.Count);
            }
        }

        public static Bowing GetBowing(int index)
        {
            return _Bowings[index];
        }
    }
}